package com.tesis_2.DA;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ParseException;


public class DAOUtils {

	private static String Server = "http://192.168.1.106:3000";

	public static String getServer() {
		return Server;
	}

	public static void setServer(String server) {
		Server = server;
	}
	
	public static Bitmap downloadFile(String fileUrl){
		
        URL myFileUrl =null;
        Bitmap bmImg = null;
        
        try {
             myFileUrl= new URL(fileUrl);
        } catch (MalformedURLException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
        }
        try {
             HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
             conn.setDoInput(true);
             conn.connect();
             InputStream is = conn.getInputStream();
             
             bmImg = BitmapFactory.decodeStream(is);
        } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
             return bmImg;
        }
        
        return bmImg;
   }
	
	public static Date parseDateIso(String date) throws java.text.ParseException {
	    SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", new Locale("es_ES"));
	    //2012-12-11T22:52:43Z
	    Date dateObj = new Date();
	   
	    //if (date.contains("T")) date = date.replace("T","'T'");
	    if (date.contains("Z")) date = date.replace("Z",".000+0000");
	    try {
	      dateObj = curFormater.parse(date);
	    } catch (ParseException e) {
	      e.printStackTrace();
	    }
	    dateObj.setHours(dateObj.getHours()+5);
	    return dateObj;
	}
	
	public static Date parseDateYyyyMmDd(String date) throws java.text.ParseException {
	    SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd", new Locale("es_ES"));
	    //2012-12-11T22:52:43Z
	    Date dateObj = new Date();
	    //if (date.contains("T")) date = date.replace("T","'T'");
	    if (date.contains("Z")) date = date.replace("Z",".000+0000");
	    try {
	      dateObj = curFormater.parse(date);
	    } catch (ParseException e) {
	      e.printStackTrace();
	    }
	    return dateObj;
	}
	
	
}
